package april.fastjson.propertyfilter;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializeWriter;

public class PropertyFilterDemo {

	public static void main(String[] args) {

		// PropertyFilter filter = new PropertyFilter() {
		//
		// @Override
		// public boolean apply(Object source, String name, Object value) {
		// return false;
		// }
		// };

		PropertyFilter filter = new PropertyFilter() {

			@Override
			public boolean apply(Object source, String name, Object value) {
				if ("name".equals(name)) {
					return true;
				}
				return false;
			}
		};

		SerializeWriter out = new SerializeWriter();
		JSONSerializer serializer = new JSONSerializer(out);
		serializer.getPropertyFilters().add(filter);

		A a = new A();
		a.setId(0);
		a.setName("andy");

		serializer.write(a);

		String text = out.toString();
		System.out.println(text);

	}

	public static class A {
		private int id;
		private String name;

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

}
